<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\OrderRefund as OrderRefundModel;
use app\common\model\Order as OrderModel;
use app\common\model\GoodsGroup as GoodsGroupModel;

/**
 * OrderRefund 订单退款控制器
 * Class OrderRefund
 * @package app\home\controller
 */
class OrderRefund extends Base
{
    /**
     * [getIndex  获取退款和关闭订单]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new OrderRefundModel;
        $omodel = new OrderModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            $time = time() - 3600*6;
            $memid = "`o`.`memid` = ".$user['id'] ." AND ";
            $field = 'o.id,o.create_time,o.status,o.price,o.gotime,g.title,g.cover,go.person_num,go.day,go.marketprice,o.num,o.refundid,o.type';
            $count = $omodel->getCounts($memid,$time);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $omodel->getlists($memid,$time,$this->from, $this->size,$field);
            foreach($lists as &$v){
                if($v['refundid'] == 0){
                    $v['state'] = '已关闭';
                }else{
                    $refund = $model->findData($v['refundid'],'id','status');
                    if($refund['status'] == 0){
                        $v['state'] = '退款申请中';
                    }elseif($refund['status'] == 1){
                        $v['state'] = '已退款';
                    }elseif($refund['status'] == 2){
                        $v['state'] = '已拒绝退款';
                    }
                }
                $v['cover'] = explode(',',$v['cover']);
                $v['cover'] = $v['cover'][0];
                $v['gotime'] = date('Y-m-d',$v['gotime']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  申请退款]
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new OrderRefundModel();
        $omodel = new OrderModel;
        $ggmodel = new GoodsGroupModel;
        try{
            $refund = $model->findData($param['orderid'],'orderid','id,status');
            $order = $omodel->findData($param['orderid'],'id','groupid');
            if($order['groupid'] > 0){
                $group = $ggmodel->findData($order['groupid'],'id','addtime,status');
                if($group['status'] != 2){
                    if(time() - $group['addtime'] < 24*3600){
                        return admin_json(-2, '拼团中无法取消订单，发起拼团24小时后，若未拼团成功将自动取消订单并退款哦');
                    }
                }
            }

            if($refund['status'] === 0){
                return admin_json(-2, '退款正在审核中！');
            }elseif($refund['status'] === 1){
                return admin_json(-3, '已退款成功！');
            }elseif($refund['status'] === 3 || $refund['status'] === 2){
                $param['id'] = $refund['id'];
                $param['status'] = 0;
                $param['create_time'] = time();
                $model->editData($param);
                $omodel->editData(['id'=>$param['orderid'],'refundid'=>$refund['id']]);
            }
            if(empty($refund)){
                $order = $omodel->findData($param['orderid'],'id','price');
                $param['orderprice'] = $order['price'];
                $param['applyprice'] = $order['price'];
                $param['refundno'] = 'SR'.date('YmdHis').randString();
                $id = $model->addData($param);
                $omodel->editData(['id'=>$param['orderid'],'refundid'=>$id]);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '申请失败'.$e->getMessage());
        }
        return admin_json(1, '申请成功');
    }
    /**
     * [postEdit  取消退款]
     * @return json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new OrderRefundModel();
        $omodel = new OrderModel();
        try{
            $order = $omodel->findData($param['id'],'id','refundid');
            $model->editData(['status'=>3,'id'=>$order['refundid']]);
            $omodel->editData(['id'=>$param['id'],'refundid'=>0]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '取消失败'.$e->getMessage());
        }
        return admin_json(1, '取消成功');
    }
}